﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
using Newtonsoft.Json;

public class Revision {

    private class RevisionDetail
    {
        public int version;
        public string touchTime;
    }

    private class RevisionData
    {
        public int latestRevision;
        public List<RevisionDetail> revisionList = new List<RevisionDetail>();
    }

    private static Revision instance = null;

    private RevisionData revisionData = null;

    private static string revisionFilePath = null;

    public static Revision Instance()
    {
        if (instance == null)
        {
            instance = new Revision();
        }

        return instance;
    }

    public void SetPath(string filePath)
    {
        revisionFilePath = filePath;
    }

    public bool Prepare(bool firstRevision)
    {
        if (firstRevision)
        {
            //RevisionDetail detail = new RevisionDetail();
            revisionData = new RevisionData();
            revisionData.latestRevision = PublishConfig.Instance().GetRevisionBase();
            //revisionData.revisionList.Add(detail);
        }
        else
        {
            string content = File.ReadAllText(revisionFilePath);
            revisionData = JsonConvert.DeserializeObject<RevisionData>(content);
        }

        return true;
    }

    public int GetLatestRevision()
    {
        return revisionData.latestRevision;
    }

    public bool CreateFile()
    {
        List<RevisionDetail> list = new List<RevisionDetail>();
        RevisionDetail detail = new RevisionDetail();
        detail.touchTime = DateTime.Now.ToString();
        detail.version = 1;
        list.Add(detail);

        RevisionData revisionData = new RevisionData();
        revisionData.latestRevision = 1;
        revisionData.revisionList = list;
        string json = JsonConvert.SerializeObject(revisionData, Formatting.None);
        File.WriteAllText(Application.dataPath + revisionFilePath, json);
        return true;
    }

    // firstRevision is true, means no resion file already exist
    public bool UpdateLatestRevionAndSave(int revision, bool firstRevision)
    {
        revisionData.latestRevision = revision;
        RevisionDetail detail = new RevisionDetail();
        detail.touchTime = DateTime.Now.ToString();
        detail.version = revision;
        revisionData.revisionList.Add(detail);

        string content = JsonConvert.SerializeObject(revisionData, Formatting.None);

        try
        {
            if (!firstRevision) {
                // backup old file
                string suffix = DateTime.Now.ToString("s").Replace(":", "_");
                string backupFilePath = revisionFilePath + "_backup_" + suffix;
                File.Copy(revisionFilePath, backupFilePath);
            }

            // write new
            StreamWriter writer = File.CreateText(revisionFilePath);
            writer.Write(content);
            writer.Flush();
            writer.Close();
            writer.Dispose();
        }
        catch(Exception e)
        {
            Debug.Log("更新热更新包描述文件：[" + revisionFilePath + "] 发生错误。错误信息：" + e.ToString());
            return false;
        }

        return true;
    }
}
